home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / win_u_z / vlistsam.zip / DLGVLIST.C next >
C/C++ Source or Header  |  1992-10-13  |  11KB  |  372 lines

  1. #include "windows.h"
  2. #include "windowsx.h"
  3. #include "custcntl.h"
  4. #include "string.h"
  5.  
  6. #include "vlistint.h"
  7. #include "dlgvlist.h"
  8.  
  9.  
  10. typedef struct StylesTable {
  11.    DWORD StyleBit;
  12.    char  szText[32];
  13.    int   nTextLength;
  14.    WORD  wCtlId;
  15. } STYLETABLE;
  16.  
  17. STYLETABLE   MyStyles[] = {
  18.                 {VLBS_NOTIFY,            "VLBS_NOTIFY",            11, DLG_VLBS_NOTIFY},
  19.                 {VLBS_USETABSTOPS,       "VLBS_USETABSTOPS",       16, DLG_VLBS_USETABSTOPS},
  20.                 {VLBS_NOREDRAW,          "VLBS_NOREDRAW",          13, DLG_VLBS_NOREDRAW},
  21.                 {VLBS_DISABLENOSCROLL,   "VLBS_DISABLENOSCROLL",   19, DLG_VLBS_DISABLENOSCROLL},
  22.                 {VLBS_NOINTEGRALHEIGHT,  "VLBS_NOINTEGRALHEIGHT",  20, DLG_VLBS_NOINTEGRALHEIGHT},
  23.                 {VLBS_WANTKEYBOARDINPUT, "VLBS_WANTKEYBOARDINPUT", 21, DLG_VLBS_WANTKEYBOARDINPUT},
  24.                 {VLBS_OWNERDRAWFIXED,    "VLBS_OWNERDRAWFIXED",    20, DLG_VLBS_OWNERDRAWFIXED},
  25.                 {VLBS_HASSTRINGS,        "VLBS_HASSTRINGS",        14, DLG_VLBS_HASSTRINGS},
  26.                 {0,0,"",0}
  27.              };
  28.  
  29. STYLETABLE   WinStyles[] = {
  30.                 {WS_BORDER,   "", 0, DLG_WS_BORDER},
  31.                 {WS_VSCROLL,  "", 0, DLG_WS_VSCROLL},
  32.                 {WS_HSCROLL,  "", 0, DLG_WS_HSCROLL},
  33.                 {WS_VISIBLE,  "", 0, DLG_WS_VISIBLE},
  34.                 {WS_DISABLED, "", 0, DLG_WS_DISABLED},
  35.                 {WS_GROUP,    "", 0, DLG_WS_GROUP},
  36.                 {WS_TABSTOP,  "", 0, DLG_WS_TABSTOP},
  37.                 {0,0,"",0}
  38.              };
  39.  
  40.  
  41. static VOID NEAR CenterWindow(HWND hwnd);
  42. static VOID NEAR ReadStyles(HWND hwnd, STYLETABLE *pStyle, LPCTLSTYLE lpCtlStyle);
  43. static VOID NEAR WriteStyles(HWND hwnd, STYLETABLE *pStyle, LPCTLSTYLE lpCtlStyle);
  44.  
  45. BOOL _export _loadds FAR PASCAL StylesDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LONG lParam);
  46.  
  47. //
  48. //  Routines to integrate VLIST with the Dialog Editor
  49. //
  50. //
  51.  
  52.  
  53. //
  54. // Information about the control
  55. //
  56.  
  57. HANDLE _export _loadds FAR PASCAL  VListInfo ()
  58. {
  59.    HANDLE    hCtlInfo;
  60.    LPCTLINFO lpCtlInfo;
  61.  
  62.    // allocate space for information structure
  63.  
  64.    hCtlInfo = GlobalAlloc(GHND, (DWORD)sizeof(CTLINFO));
  65.    if (hCtlInfo) {
  66.       lpCtlInfo = (LPCTLINFO)GlobalLock(hCtlInfo);
  67.       if (lpCtlInfo) {
  68.          lpCtlInfo->wVersion = 100;
  69.  
  70.          _fstrcpy(lpCtlInfo->szClass, "VLIST");
  71.          _fstrcpy(lpCtlInfo->szTitle, "Virtual List Box");
  72.  
  73.          lpCtlInfo->wCtlTypes = 1;
  74.          lpCtlInfo->Type[0].wWidth =  50;
  75.          lpCtlInfo->Type[0].wHeight = 50;
  76.          lpCtlInfo->Type[0].dwStyle = WS_CHILD | WS_BORDER | VLBS_NOINTEGRALHEIGHT;
  77.          _fstrcpy(lpCtlInfo->Type[0].szDescr, "Virtual List Box");
  78.  
  79.          GlobalUnlock(hCtlInfo);
  80.       }
  81.       else
  82.       {
  83.          GlobalFree(hCtlInfo);
  84.          hCtlInfo = NULL;
  85.       }
  86.    }
  87.  
  88.    return (hCtlInfo);
  89. }
  90.  
  91.  
  92. //
  93. // Translate the style bits to strings for the dialog (.dlg) file
  94. //
  95. WORD _export _loadds FAR PASCAL VlistFlags (WORD wFlags, LPSTR lpszString, WORD wMaxString)
  96. {
  97.    WORD wLength;
  98.    STYLETABLE *pStyle;
  99.  
  100.    lpszString[0] = 0;
  101.    wLength = 0;
  102.  
  103.    for ( pStyle = &MyStyles[0]; pStyle->StyleBit != 0L; pStyle++ ) {
  104.       if ( wFlags & pStyle->StyleBit && wMaxString > wLength+pStyle->nTextLength+3 ) {
  105.          if ( wLength ) {
  106.             _fstrcat(lpszString," | ");
  107.             wLength += 3;
  108.          }
  109.          _fstrcat(lpszString, pStyle->szText);
  110.          wLength += pStyle->nTextLength;
  111.       }
  112.    }
  113.  
  114.    return wLength;
  115. }
  116.  
  117.  
  118. //
  119. // A Dialog box to set the control's style bits.
  120. //
  121. //
  122.  
  123. //
  124. // A function the dialog editor can call which will envoke the dialog box
  125. //
  126. //
  127. BOOL _export _loadds FAR PASCAL VlistStyle(HWND hWnd, HANDLE hCtlStyle,
  128.                            LPFNSTRTOID lpfnStrToId, LPFNIDTOSTR lpfnIdToStr)
  129. {
  130.    int        iRet;
  131.    HWND       hwndLook;
  132.  
  133.    // HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK
  134.    //
  135.    // Disable the Dialog editor's owned windows so Custom Controls
  136.    // act like built in controls.
  137.    //
  138.    // HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK
  139.    //
  140.    // Get the first top level window
  141.    //
  142.    hwndLook = GetWindow(GetDesktopWindow(), GW_CHILD);
  143.    while ( hwndLook ) {
  144.       if ( GetParent(hwndLook) == hWnd )
  145.          EnableWindow(hwndLook, FALSE);
  146.       //
  147.       // Get the next top level window
  148.       //
  149.       hwndLook = GetWindow(hwndLook, GW_HWNDNEXT);
  150.    }
  151.  
  152.    iRet=DialogBoxParam(hInstance, MAKEINTRESOURCE(100), hWnd, StylesDlgProc,
  153.                        MAKELPARAM(hCtlStyle,0));
  154.  
  155.    // HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK
  156.    //
  157.    // Enable the Dialog editor's owned windows
  158.    //
  159.    // HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK
  160.    //
  161.    hwndLook = GetWindow(GetDesktopWindow(), GW_CHILD);
  162.    while ( hwndLook ) {
  163.       if ( GetParent(hwndLook) == hWnd )
  164.          EnableWindow(hwndLook, TRUE);
  165.       hwndLook = GetWindow(hwndLook, GW_HWNDNEXT);
  166.    }
  167.  
  168.    return (iRet);
  169. }
  170.  
  171. //
  172. // The dialog proc for the dialog box
  173. //
  174. //
  175. BOOL _export _loadds FAR PASCAL StylesDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LONG lParam)
  176. {
  177.     HANDLE hCtlStyle;
  178.     LPCTLSTYLE lpCtlStyle;
  179.  
  180.     switch (msg) {
  181.         case WM_INITDIALOG:
  182.             //
  183.             // Put Handle in Window Property
  184.             //
  185.             hCtlStyle = LOWORD(lParam);
  186.             SetProp(hwnd, MAKEINTRESOURCE(1), hCtlStyle);
  187.  
  188.             lpCtlStyle = (LPCTLSTYLE )GlobalLock(hCtlStyle);
  189.  
  190.             //
  191.             // Interpret current styles
  192.             //
  193.  
  194.             //
  195.             // control specific styles
  196.             //
  197.             ReadStyles(hwnd, MyStyles, lpCtlStyle);
  198.  
  199.             //
  200.             // Windows styles
  201.             //
  202.             ReadStyles(hwnd, WinStyles, lpCtlStyle);
  203.  
  204.             //
  205.             // Do special case items
  206.             //
  207.             // i.e. can't have VLBS_HASSTRINGS without VLBS_OWNERDRAWFIXED
  208.             //
  209.             if ( lpCtlStyle->dwStyle & VLBS_OWNERDRAWFIXED ) {
  210.                Button_Enable(GetDlgItem(hwnd,DLG_VLBS_HASSTRINGS), TRUE);
  211.             }
  212.             else {
  213.                Button_SetCheck(GetDlgItem(hwnd,DLG_VLBS_HASSTRINGS), FALSE);
  214.                Button_Enable(GetDlgItem(hwnd,DLG_VLBS_HASSTRINGS), FALSE);
  215.             }
  216.  
  217.             GlobalUnlock(hCtlStyle);
  218.  
  219.             CenterWindow(hwnd);
  220.  
  221.             return TRUE;
  222.  
  223.         case WM_COMMAND:
  224.             switch (wParam) {
  225.                 case DLG_WS_VSCROLL:
  226.                      Button_SetCheck(GetDlgItem(hwnd,DLG_WS_HSCROLL), Button_GetCheck(GetDlgItem(hwnd,DLG_WS_VSCROLL)));
  227.                    break;
  228.  
  229.                 case DLG_WS_HSCROLL:
  230.                      Button_SetCheck(GetDlgItem(hwnd,DLG_WS_VSCROLL), Button_GetCheck(GetDlgItem(hwnd,DLG_WS_HSCROLL)));
  231.                    break;
  232.  
  233.                 case DLG_VLBS_OWNERDRAWFIXED:
  234.                    if (Button_GetCheck(GetDlgItem(hwnd,DLG_VLBS_OWNERDRAWFIXED))) {
  235.                       Button_Enable(GetDlgItem(hwnd,DLG_VLBS_HASSTRINGS), TRUE);
  236.                    }
  237.                    else {
  238.                       Button_SetCheck(GetDlgItem(hwnd,DLG_VLBS_HASSTRINGS), FALSE);
  239.                       Button_Enable(GetDlgItem(hwnd,DLG_VLBS_HASSTRINGS), FALSE);
  240.                    }
  241.  
  242.                    break;
  243.  
  244.                 case IDOK:
  245.                     //
  246.                     // Get the style structure handle
  247.                     //
  248.                     hCtlStyle = GetProp(hwnd, MAKEINTRESOURCE(1));
  249.                     lpCtlStyle = (LPCTLSTYLE )GlobalLock(hCtlStyle);
  250.  
  251.                     //
  252.                     // Interpret new styles
  253.                     //
  254.  
  255.                     //
  256.                     // control specific styles
  257.                     //
  258.                     WriteStyles(hwnd, MyStyles, lpCtlStyle);
  259.  
  260.                     //
  261.                     // Windows  styles
  262.                     //
  263.                     WriteStyles(hwnd, WinStyles, lpCtlStyle);
  264.  
  265.                     GlobalUnlock(hCtlStyle);
  266.  
  267.                     RemoveProp(hwnd, MAKEINTRESOURCE(1));
  268.                     EndDialog(hwnd, IDOK);
  269.                     return TRUE;
  270.  
  271.                 case IDCANCEL:
  272.                     RemoveProp(hwnd, MAKEINTRESOURCE(1));
  273.                     EndDialog(hwnd, IDCANCEL);
  274.                     return FALSE;
  275.  
  276.             }
  277.  
  278.             return FALSE;
  279.  
  280.         default:
  281.             return FALSE;
  282.     }
  283. }
  284.  
  285. //
  286. // Read the style bit, if bit is set, set control on, otherwise set it off
  287. //
  288. static VOID NEAR ReadStyles(HWND hwnd, STYLETABLE *pStyle, LPCTLSTYLE lpCtlStyle)
  289. {
  290.    for ( ; pStyle->StyleBit != 0L; pStyle++ ) {
  291.       if ( lpCtlStyle->dwStyle & pStyle->StyleBit )
  292.           Button_SetCheck(GetDlgItem(hwnd,pStyle->wCtlId), TRUE);
  293.       else
  294.           Button_SetCheck(GetDlgItem(hwnd,pStyle->wCtlId), FALSE);
  295.    }
  296. }
  297.  
  298. //
  299. // Read the control, if control on, set bit, otherwise clear bit
  300. //
  301. static VOID NEAR WriteStyles(HWND hwnd, STYLETABLE *pStyle, LPCTLSTYLE lpCtlStyle)
  302. {
  303.    for ( ; pStyle->StyleBit != 0L; pStyle++ ) {
  304.        if ( Button_GetCheck(GetDlgItem(hwnd,pStyle->wCtlId)))
  305.            lpCtlStyle->dwStyle |= pStyle->StyleBit;
  306.        else
  307.            lpCtlStyle->dwStyle &= ~pStyle->StyleBit;
  308.    }
  309. }
  310.  
  311. static VOID NEAR CenterWindow(HWND hwnd)
  312. {
  313.     RECT rc;
  314.     RECT rcOwner;
  315.     RECT rcCenter;
  316.     HWND hwndOwner;
  317.     int cxScreen;
  318.     int cyScreen;
  319.     int delta;
  320.  
  321.     GetWindowRect(hwnd, &rc);
  322.  
  323.     if (!(hwndOwner = GetWindow(hwnd, GW_OWNER)))
  324.         hwndOwner = GetDesktopWindow();
  325.  
  326.     GetWindowRect(hwndOwner, &rcOwner);
  327.  
  328.     rcCenter.left = rcOwner.left +
  329.             (((rcOwner.right - rcOwner.left) -
  330.             (rc.right - rc.left))
  331.             / 2);
  332.  
  333.     rcCenter.top = rcOwner.top +
  334.             (((rcOwner.bottom - rcOwner.top) -
  335.             (rc.bottom - rc.top))
  336.             / 2);
  337.  
  338.     rcCenter.right = rcCenter.left + (rc.right - rc.left);
  339.     rcCenter.bottom = rcCenter.top + (rc.bottom - rc.top);
  340.  
  341.     cxScreen = GetSystemMetrics(SM_CXSCREEN);
  342.  
  343.     if (rcCenter.right > cxScreen) {
  344.         delta = rcCenter.right - rcCenter.left;
  345.         rcCenter.right = cxScreen;
  346.         rcCenter.left = rcCenter.right - delta;
  347.     }
  348.  
  349.     if (rcCenter.left < 0) {
  350.         delta = rcCenter.right - rcCenter.left;
  351.         rcCenter.left = 0;
  352.         rcCenter.right = rcCenter.left + delta;
  353.     }
  354.  
  355.     cyScreen = GetSystemMetrics(SM_CYSCREEN);
  356.  
  357.     if (rcCenter.bottom > cyScreen) {
  358.         delta = rcCenter.bottom - rcCenter.top;
  359.         rcCenter.bottom = cyScreen;
  360.         rcCenter.top = rcCenter.bottom - delta;
  361.     }
  362.  
  363.     if (rcCenter.top < 0) {
  364.         delta = rcCenter.bottom - rcCenter.top;
  365.         rcCenter.top = 0;
  366.         rcCenter.bottom = rcCenter.top + delta;
  367.     }
  368.  
  369.     SetWindowPos(hwnd, NULL, rcCenter.left, rcCenter.top, 0, 0,
  370.             SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
  371. }
  372.